package com.study.view.widget;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;

import com.study.view.R;
import com.study.view.util.UtilView;

import androidx.annotation.Nullable;

/**
 * author : xujianhong
 * date   : 2022/8/19
 * desc   : 圆形进度条
 * version: 1.0
 */
public class CircularProgress extends View{

    private static final String TAG = "CircularProgress";

    // 文字画笔
    Paint textPaint;
    // 弧形背景画笔
    Paint backProgressPaint;
    // 弧形进度画笔
    Paint progressPaint;
    //进度
    String mProgress = "0%";

    int textColor = R.color.color_009ad6;

    int backProgressColor = R.color.color_90d7ec;

    int progressColor = R.color.color_009ad6;

    //圆弧的宽度
    float progressWidgt = UtilView.dip2px(20);

    Context mContext;

    int sweepAngle = 0;

    ValueAnimator valueAnimator = null;

    public CircularProgress(Context context) {
        this(context,null);
    }

    public CircularProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircularProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        textPaint = initPaint(textColor);
        textPaint.setTextSize(UtilView.sp2px(18));
        textPaint.setTypeface(Typeface.DEFAULT_BOLD);

        backProgressPaint = initPaint(backProgressColor);
        backProgressPaint.setStrokeWidth(progressWidgt);
        backProgressPaint.setStyle(Paint.Style.STROKE);
        backProgressPaint.setColor(Color.parseColor("#90d7ec"));

        progressPaint = initPaint(progressColor);
        progressPaint.setStrokeWidth(progressWidgt);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setColor(Color.parseColor("#009ad6"));
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
    }

    private Paint initPaint(int color) {
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(color);
        return paint;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(Math.min(width,height),Math.min(width,height));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        float left = 0 + progressWidgt / 2;
        float top = 0 + progressWidgt / 2;
        float right = getWidth() - progressWidgt / 2;
        float bottom = getHeight() - progressWidgt / 2;
        //画背景弧
        canvas.drawArc(left, top,right, bottom, 0,
        360, false, backProgressPaint);

        //进度弧
        canvas.drawArc(left, top,right, bottom, -90,
                sweepAngle, false, progressPaint);

        Rect rect = new Rect();
        textPaint.getTextBounds(mProgress,0,mProgress.length(),rect);
        float dx = getWidth()/2-rect.width()/2;
        // 获取基线
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        int dy = (int) ((fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom);
        int baseLine = getHeight() / 2 + dy;

        canvas.drawText(mProgress,dx, baseLine,textPaint);


    }


    public void setStateProgress(){
        startAnimator();
    }

    private void startAnimator(){
        valueAnimator = ValueAnimator.ofFloat(1);
        valueAnimator.setDuration(4000);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animationValue = (float) animation.getAnimatedValue();
                Log.d(TAG, "onAnimationUpdate: "+animationValue);
                sweepAngle = (int) (360 * animationValue);
                mProgress =  (int) (animationValue * 100) + "%";
                invalidate();
            }
        });
        valueAnimator.start();
    }

    public void stopAnimator(){
        if(valueAnimator != null){
            valueAnimator.cancel();
            valueAnimator = null;
        }
    }

}
